/**
  **************************************************************************
  * @file     iap.c
  * @brief    iap program
  **************************************************************************
  *                       Copyright notice & Disclaimer
  *
  * The software Board Support Package (BSP) that is made available to
  * download from Artery official website is the copyrighted work of Artery.
  * Artery authorizes customers to use, copy, and distribute the BSP
  * software and its related documentation for the purpose of design and
  * development in conjunction with Artery microcontrollers. Use of the
  * software is governed by this copyright notice and the following disclaimer.
  *
  * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
  * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
  * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
  * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
  * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
  *
  **************************************************************************
  */

#include "flapp_factory.h"
#include "fldrv_uart.h"
#include "flapp_system.h"
#include "flapp_net_slm.h"
#include "flapp_peripheral.h"
#include "flapp_flash.h"

typedef struct 
{			
	u8 u8WaitTime;					//发送标志														
}flapp_factory_para_t;	
flapp_factory_para_t factory_para;

void flapp_wait_net_response()
{
	if(factory_para.u8WaitTime == FALSE)	return;
	
	factory_para.u8WaitTime--;
	if(factory_para.u8WaitTime == FALSE)
	{
		u8 csq;
		csq = flapp_get_csq_status();
		if(csq >= 20)
			LED_SUB_VIS_ON;
		else						
			LED_SUB_VIS_OFF;
	}
}
/**
* @brief  解析设备数据
  * @param  void
  * @retval none
  */
void flapp_factory_rcv_run(void)
{
	char *p;
	static u16 u16WaitExitTestMode = FALSE;
	flapp_sys_para_t factory_sys_para;
	fldrv_uart_rx_t tNetRcvBuf;
	flapp_wait_net_response();
	tNetRcvBuf = fldrv_uart1_rcv_time_out_check();		//串口接收检测，接收超时就处理数据
	factory_sys_para = flapp_get_sys_para();
	if(factory_sys_para.bTestMode)
	{
		u16WaitExitTestMode++;
		if(u16WaitExitTestMode > 7500)			//30秒无动作自动退出厂测
		{
			COM_PRINTF("exit test mode on timeout");
			u16WaitExitTestMode = FALSE;
			factory_sys_para.bTestMode = FALSE;
			flapp_set_sys_para(factory_sys_para);
		}
	}
	
	if(tNetRcvBuf.u16UartRxLen < UART_RCV_MIN_LEN)	return;
	
	
	if(factory_sys_para.bPowOnOff == FALSE)
		return;
	
	if(strstr((char*)tNetRcvBuf.u8UartRxBuf,FACTORY_ENABLE))	//打开厂测
	{
		u16WaitExitTestMode = FALSE;
		COM_PRINTF("enter factory test\r\n");
		COM_PRINTF("version:%s\r\n",VERSION);
		factory_sys_para.bTestMode = TRUE;
		flapp_set_sys_para(factory_sys_para);
		return;
	}
	if(strstr((char*)tNetRcvBuf.u8UartRxBuf,FACTORY_DISABLE))	//关闭厂测
	{
		u16WaitExitTestMode = FALSE;
		COM_PRINTF("close factory\r\n");
		factory_sys_para.bTestMode = FALSE;
		flapp_set_sys_para(factory_sys_para);
		return;
	}
	
	if(factory_sys_para.bTestMode == FALSE)
		return;
	
	if(strstr((char*)tNetRcvBuf.u8UartRxBuf,FACTORY_LED_ON_TEST))	//灯光全开
	{
		u16WaitExitTestMode = FALSE;
		LED_NET_BAD_ON;
		LED_SUB_VIS_ON;
		LED_LOW_POW_ON;
		LED_MED_ALA_ON;
		LED_CHAR_FULL_ON;
		LED_NET_OK_ON;
		COM_PRINTF("open all led\r\n");
	}
	if(strstr((char*)tNetRcvBuf.u8UartRxBuf,FACTORY_LED_OFF_TEST))	//灯光全关
	{
		u16WaitExitTestMode = FALSE;
		LED_NET_BAD_OFF;
		LED_SUB_VIS_OFF;
		LED_LOW_POW_OFF;
		LED_MED_ALA_OFF;
		LED_CHAR_FULL_OFF;
		LED_NET_OK_OFF;
		COM_PRINTF("close all led\r\n");
	}
	if(strstr((char*)tNetRcvBuf.u8UartRxBuf,FACTORY_HALL_TEST))	//霍尔测试
	{
		u16WaitExitTestMode = FALSE;
		if(RD_HALL_IN)	
			LED_MED_ALA_OFF;	
		else						
			LED_MED_ALA_ON;	
		COM_PRINTF("hall test\r\n");
	}
	if(strstr((char*)tNetRcvBuf.u8UartRxBuf,FACTORY_NET_TEST))	//网络测试
	{
		u16WaitExitTestMode = FALSE;
		if(RD_HALL_IN)	LED_MED_ALA_OFF;	
		else						LED_MED_ALA_ON;	
		factory_para.u8WaitTime = 25;
		flapp_insert_inquire_cmd(AT_STEP_TEST_CSQ);
		COM_PRINTF("net test\r\n");
	}
	if(strstr((char*)tNetRcvBuf.u8UartRxBuf,FACTORY_SIM_TEST))	//ICCID测试
	{
		u32 id;
		id = flapp_get_id_status();
		flapp_insert_inquire_cmd(AT_STEP_TEST_ICCID);
		COM_PRINTF("iccid test ok\r\n");
		u16WaitExitTestMode = FALSE;
		COM_PRINTF(FACTORY_UUID,id);
	}
	if(strstr((char*)tNetRcvBuf.u8UartRxBuf,FACTORY_BUZ_TEST))	//语音测试
	{
		u16WaitExitTestMode = FALSE;
		flapp_set_play_command(VOICE_BEST_WISH,KEY_NUM_ONE);
		COM_PRINTF("voice test\r\n");
	}
	
	p = strstr((char*)tNetRcvBuf.u8UartRxBuf,FACTORY_SET_UUID);
	if(p)	//写ID
	{
		u32 uuid = flapp_get_dec_num_from_str(p + strlen(FACTORY_SET_UUID),'\t');
		if((uuid >= 10000000) && (uuid <= 99999999)){
			u16 uuid_set_buf[4];
			uuid_set_buf[0] = (u32)FLASH_SAVE_HEAD_ADDR >> 16;
			uuid_set_buf[1] = (u16)FLASH_SAVE_HEAD_ADDR;
			uuid_set_buf[2] = uuid >> 16;
			uuid_set_buf[3] = uuid;
			flash_write(FLASH_SET_PARA_ADDR,uuid_set_buf,4);
			COM_PRINTF("wirte id success,id is:%d\r\n",uuid);
		}
		else{
			COM_PRINTF("wirte id failure\r\n");
		}
		u16WaitExitTestMode = FALSE;
	}
	
}

